#if !defined(__APP_SERIAL_H__)
#define __APP_SERIAL_H__

#include "app_device.h"
#include <termio.h>

typedef enum
{
    BaudRate_9600 = B9600,
    BaudRate_19200 = B19200,
    BaudRate_38400 = B38400,
    BaudRate_57600 = B57600,
    BaudRate_115200 = B115200,
} BaudRate;

typedef enum
{
    Parity_None = 0,
    Parity_Odd = PARENB | PARODD,
    Parity_Even = PARENB,
} Parity;

typedef enum
{
    DataBits_5 = CS5,
    DataBits_6 = CS6,
    DataBits_7 = CS7,
    DataBits_8 = CS8,
} DataBits;

typedef enum
{
    StopBits_1 = 0,
    StopBits_2 = CSTOPB,
} StopBits;

typedef struct
{
    Device super;
    BaudRate baud_rate;
    Parity parity;
    DataBits data_bits;
    StopBits stop_bits;
} SerialDevice;

int app_serial_init(SerialDevice *device, char *filename);

int app_serial_setBaudRate(SerialDevice *device, BaudRate baud_rate);

int app_serial_setParity(SerialDevice *device, Parity parity);

int app_serial_setDataBits(SerialDevice *device, DataBits data_bits);

int app_serial_setStopBits(SerialDevice *device, StopBits stop_bits);

/**
 * @brief 设置阻塞模式
 * 
 * @param device 串口
 * @param enable 1 阻塞模式，0 非阻塞模式
 * @return int 0 成功 -1 失败
 */
int app_serial_setBlockMode(SerialDevice *device, int enable);

int app_serial_flush(SerialDevice *device);

#endif // __APP_SERIAL_H__
